package com.situ.mall.controller;

import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.Cart;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.service.ICartService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Cart cart, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Integer productId = cart.getProductId();
        Cart cartPD =cartService.selectByProductId(userId, productId);
        if (cartPD != null) {
            Integer id = cartPD.getId();
            cart.setId(id);
            cartService.updateNum(cart);
            return Result.ok("添加成功");
        }
        cart.setUserId(userId);
        cart.setChecked(1);
        System.out.println(cart);
        cartService.add(cart);
        return Result.ok("添加成功");
    }
    @RequestMapping("/cartListSelectById")
    @ResponseBody
    public Result cartListSelectById(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<CartVO> cartVO = cartService.cartListSelectById(userId);
        return Result.ok(cartVO);
    }
    @RequestMapping("/cartListSelectByIdChecked")
    @ResponseBody
    public Result cartListSelectByIdChecked(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<CartVO> cartVO = cartService.cartListSelectByIdChecked(userId);
        return Result.ok(cartVO);
    }
    @RequestMapping("/checkedAll")
    @ResponseBody
    public Result checkedAll(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Integer count = cartService.checkedAll(userId);
        System.out.println("count:"+count);
        return Result.ok(count);
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Cart cart) {
        System.out.println(cart);
        cartService.update(cart);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/updateAll")
    @ResponseBody
    public Result updateAll(Cart cart, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        cart.setUserId(userId);
        cartService.updateAll(cart);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        cartService.deleteById(id);
        return Result.ok("删除成功！");
    }
}
